package com.vma.suggestion.service;

import com.jfinal.plugin.activerecord.Page;
import com.vma.shoes.bizModel.model.Suggestion;
import com.vma.shoes.bizModel.model.User;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by chenzui on 2017/4/25.
 */
public class SuggestionService {
    public static Page<Suggestion> qrySuggestionList(Integer cPage, Integer pSize, String status, String phone, String start, String end){
        String condition = "";
        List<String> param = new ArrayList<String>();
        if(status != null && !"".equals(status)){
            condition += "and status = ? ";
            param.add(status);

        }
        if(start != null && !"".equals(start)){
            condition += "and create_time > ? ";
            param.add(start);
        }
        if(end != null && !"".equals(end)){
            condition += "and create_time < ? ";
            param.add(end);
        }
        if(phone != null && !"".equals(phone)){
            condition += "and phone like ? ";
            param.add("%"+phone+"%");
        }
        Page<Suggestion> suggestionPage = Suggestion.dao.paginate(cPage,pSize,"SELECT *"," FROM suggestion where 1=1 "+condition+" order by id desc"
                ,param.toArray());


        return suggestionPage;
    }

    public static void addSuggestion(Integer userId,String content){
        User user = User.dao.findById(userId);
        String phone = user.getPhone();
        new Suggestion().setStatus("1").setUserId(userId).setCreateTime(new Date()).setContent(content).setPhone(phone).save();
    }

    public static void delSuggestion(Integer suggestionId){
        Suggestion.dao.findById(suggestionId).setStatus("0").update();
    }
}
